package com.kuang.controller;

import com.aliyuncs.utils.StringUtils;
import com.kuang.service.SendSms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@CrossOrigin//跨域支持
public class SmsApiSend {

    @Autowired
    private SendSms sendSms;
    @Autowired
    private RedisTemplate<String,String>  redisTemplate;

    @GetMapping("/send/{phone}")
    public String code(@PathVariable("phone") String phone){
        String code = redisTemplate.opsForValue().get("phone");
        if (!StringUtils.isEmpty(code)){
            return phone+":"+code+"验证码没有国企可以继续使用";
        }
        //生成验证码
        code = UUID.randomUUID().toString().substring(0, 4);
        Map<String,Object> param = new HashMap<>();
        param.put("code",code);
        Boolean isSend = sendSms.send(phone, "SMS_218725558", param);
        if (isSend){
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
            return phone+":"+code+"发送成功";
        }else {
            return "发送失败";
        }
    }

}
